import{_ as e}from"./plugin-vue_export-helper-DlAUqK2U.js";import{o as i,c as n,f as l}from"./app-CRsXJvQX.js";const d={},s=l(`<p>线程池模式</p><p>线程池 本质一个线程安全工作队列连接工作者线程和客户端线程</p><p>实现线程池的三步 1、实现线程安全的阻塞队列 (生产者-消费者范例)</p><div class="language-text line-numbers-mode" data-ext="text" data-title="text"><pre class="language-text"><code>public interface BlockQueue&lt;E&gt;{
    void add(E e); //添加元素
    E take();      //取走元素
    E take(int timeout);
    int size(); //队列size
}

public ArrayBlockQueue&lt;E&gt; implement BlockQueue&lt;E&gt;{
    
    private List&lt;E&gt; blockList = new ArrayList();
    private Object lock = new Object();
    private volaitle int size = 0;
    
    //生产者
    public void add(E e){
        synchionized(lock){
            blockList.add(e);
            lock.notifyAll();   
        }
    }
    
    //消费者
    public E take(int timeout){
    
        synchionized(lock){
            if(timeout &lt;= 0){
                while(blockList.isEmpty()){
                    lock.wait();
                }
            }else{
                long future = System.currentTimeMills()+timeout;
                long remaining = timeout;
                while(blockList.isEmpty() &amp;&amp; remain &gt; 0){
                    lock.wait(remaining);            
                    remaining = future - System.currentTimeMills();
                }                
            }
        return blockList.get(0);
    }
    
    //消费者
    public E take(){
        return take(0);
    }
    
    //获得当前队列大小
    public int size(){
        synchionized(lock){
            size = blockList.size();  
        }
        return size;
    }
    
}


</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>2、编写执行者可安全终止的Worker</p><div class="language-text line-numbers-mode" data-ext="text" data-title="text"><pre class="language-text"><code>public Worker&lt;Job extend Runnable&gt; implement Runnable{
    private volatile boolean switchFlag = true;
    private BlockQueue&lt;Job&gt; blockQueue;
    
    public Worker(BlockQueue queue){
        blockQueue = queue;
    }
    
    public void run(){
        while(switchFlag &amp;&amp; !Thread.currentThread().isInterrupted()){
            Job job = blockQueue.take();
            job.run();
        }
    }
    
    public shutdown(){
        switchFlag = false;
    }
    
}

</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>3、线程池框架</p><div class="language-text line-numbers-mode" data-ext="text" data-title="text"><pre class="language-text"><code>线程池接口规范
public interface ThreadPool&lt;Job extends Runnable&gt;{
    //执行工作
    void execute(Job job);
    //关闭线程池
    void shutdown();
    //增加线程
    void addWorker(int num);
    //减少线程
    void removeWork(int num);
    //当前任务job数
    int getJobSize();
    //当前线程数
    int getThreadCount();
}


public SimpleThreadPool&lt;Job extends Runnable&gt; implement ThreadPool&lt;Job&gt;{

//任务阻塞队列
private BlockQueue&lt;Job&gt; blockQueue = new ArrayBlockQueue()&lt;Job&gt;;

//线程队列
private List&lt;Worker&gt; workerList = Collections.sysnchronizedList(new ArrayList&lt;Worker&gt;);

//最大线程数
private static final int MAX_WORK_NUM = 10;

//默认线程数
private static final int DEFAULT_WORK_NUM = 3;

public SimpleThreadPool(){
    addWorker(DEFAULT_WORK_NUM);
}

//执行任务
public void execute(Job job){
    blockQueue.add(job);
}

//关闭线程池
public void synchrionized shutdown(){
    for(Worker worker : workerList){
        worker.shutdown();
    }
}

//增加线程任务
public synchrionized void addWorker(int num){
   //当前线程数
   int threadNum = workerList.size();
      //限制创建线程数不能超过最大限度
      if(threadNum + num &gt; MAX_WORK_NUM){
       num = MAX_WORK_NUM - threadNum ;
   }

   for(int i = 0; i &lt; num ; i++){
        creatOneWorker();
   }
}

public synchrionized void removeWorker(int num){
    //当前线程数
   int threadNum = workerList.size();
   //最大删除数是当前线程数
   if(num &gt; threadNum){
        num = threadNum;
   }

    //移除工作线程
    for(int i = 0; i &lt; num ;i++){
        Worker worker = workerList.get(i);
        worker.shutdown();
        workerList.remove(worker);
    }

}

//当前线程数
public synchrionized int getThreadCount(){
    return workerList.size();
}

//获得当前任务数
public int getJobSize(){
    return blockQueue.size();
}

//创建一个线程
private void creatOneWorker(){
     //创建工作者
    Worker worker = new Worker(blockQueue);
    workerList.add(worker);
    Thread thread = new Thread(worker);
    thread.start();
}

}

</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>juc</p><p>ThreadPoolExecutor线程池</p><p>ThreadFactory定制线程</p><p>Executors.newFixedThreadPool() 固定线程数</p><p>Executors.newCachedThreadPool() 缓存线程池</p><p>Executors.newScheduledThreadPool() 定时线程池</p>`,14),r=[s];function a(t,v){return i(),n("div",null,r)}const m=e(d,[["render",a],["__file","Worker-Thread-模式.html.vue"]]),o=JSON.parse('{"path":"/basis/juc/java%E9%AB%98%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/Worker-Thread-%E6%A8%A1%E5%BC%8F.html","title":"Worker-Thread-模式","lang":"zh-CN","frontmatter":{"title":"Worker-Thread-模式","date":"2024-03-24T11:47:50.000Z","category":["java高并发编程"],"tag":["archive"],"description":"线程池模式 线程池 本质一个线程安全工作队列连接工作者线程和客户端线程 实现线程池的三步 1、实现线程安全的阻塞队列 (生产者-消费者范例) 2、编写执行者可安全终止的Worker 3、线程池框架 juc ThreadPoolExecutor线程池 ThreadFactory定制线程 Executors.newFixedThreadPool() 固定线...","head":[["meta",{"property":"og:url","content":"https://yinlingchaoliu.github.io/basis/juc/java%E9%AB%98%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/Worker-Thread-%E6%A8%A1%E5%BC%8F.html"}],["meta",{"property":"og:site_name","content":"引领潮流"}],["meta",{"property":"og:title","content":"Worker-Thread-模式"}],["meta",{"property":"og:description","content":"线程池模式 线程池 本质一个线程安全工作队列连接工作者线程和客户端线程 实现线程池的三步 1、实现线程安全的阻塞队列 (生产者-消费者范例) 2、编写执行者可安全终止的Worker 3、线程池框架 juc ThreadPoolExecutor线程池 ThreadFactory定制线程 Executors.newFixedThreadPool() 固定线..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:locale","content":"zh-CN"}],["meta",{"property":"og:updated_time","content":"2024-03-25T14:17:18.000Z"}],["meta",{"property":"article:author","content":"引领潮流"}],["meta",{"property":"article:tag","content":"archive"}],["meta",{"property":"article:published_time","content":"2024-03-24T11:47:50.000Z"}],["meta",{"property":"article:modified_time","content":"2024-03-25T14:17:18.000Z"}],["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"Worker-Thread-模式\\",\\"image\\":[\\"\\"],\\"datePublished\\":\\"2024-03-24T11:47:50.000Z\\",\\"dateModified\\":\\"2024-03-25T14:17:18.000Z\\",\\"author\\":[{\\"@type\\":\\"Person\\",\\"name\\":\\"引领潮流\\",\\"url\\":\\"https://www.jianshu.com/u/bdcce32c05dd\\"}]}"]]},"headers":[],"git":{"createdTime":1711376238000,"updatedTime":1711376238000,"contributors":[{"name":"引领潮流","email":"heat13@qq.com","commits":1}]},"readingTime":{"minutes":2.04,"words":612},"filePathRelative":"basis/juc/java高并发编程/Worker-Thread-模式.md","localizedDate":"2024年3月24日","excerpt":"<p>线程池模式</p>\\n<p>线程池\\n本质一个线程安全工作队列连接工作者线程和客户端线程</p>\\n<p>实现线程池的三步\\n1、实现线程安全的阻塞队列 (生产者-消费者范例)</p>\\n<div class=\\"language-text\\" data-ext=\\"text\\" data-title=\\"text\\"><pre class=\\"language-text\\"><code>public interface BlockQueue&lt;E&gt;{\\n    void add(E e); //添加元素\\n    E take();      //取走元素\\n    E take(int timeout);\\n    int size(); //队列size\\n}\\n\\npublic ArrayBlockQueue&lt;E&gt; implement BlockQueue&lt;E&gt;{\\n    \\n    private List&lt;E&gt; blockList = new ArrayList();\\n    private Object lock = new Object();\\n    private volaitle int size = 0;\\n    \\n    //生产者\\n    public void add(E e){\\n        synchionized(lock){\\n            blockList.add(e);\\n            lock.notifyAll();   \\n        }\\n    }\\n    \\n    //消费者\\n    public E take(int timeout){\\n    \\n        synchionized(lock){\\n            if(timeout &lt;= 0){\\n                while(blockList.isEmpty()){\\n                    lock.wait();\\n                }\\n            }else{\\n                long future = System.currentTimeMills()+timeout;\\n                long remaining = timeout;\\n                while(blockList.isEmpty() &amp;&amp; remain &gt; 0){\\n                    lock.wait(remaining);            \\n                    remaining = future - System.currentTimeMills();\\n                }                \\n            }\\n        return blockList.get(0);\\n    }\\n    \\n    //消费者\\n    public E take(){\\n        return take(0);\\n    }\\n    \\n    //获得当前队列大小\\n    public int size(){\\n        synchionized(lock){\\n            size = blockList.size();  \\n        }\\n        return size;\\n    }\\n    \\n}\\n\\n\\n</code></pre></div>","autoDesc":true}');export{m as comp,o as data};
